package com.capri.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.capri.R;
import com.capri.app.NurseHomeApp;
import com.capri.base.BaseActivity;
import com.capri.model.Action.OrderStatusAction;
import com.capri.model.INurse;
import com.capri.model.bean.BaseResultBean;
import com.capri.model.bean.OrderDetailBean;
import com.capri.model.impl.NurseModel;
import com.capri.utils.CommonSubscriber;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import es.dmoral.toasty.Toasty;
import rx.functions.Action0;

/**
 * 订单详情画面
 */
public class OrderDetailActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.imv_header)
    ImageView imvHeader;
    @BindView(R.id.tv_order_num)
    TextView tvOrderNum;
    @BindView(R.id.tv_service_name)
    TextView tvServiceName;
    @BindView(R.id.tv_order_status)
    TextView tvOrderStatus;
    @BindView(R.id.tv_patient_name)
    TextView tvPatientName;
    @BindView(R.id.tv_patient_age)
    TextView tvPatientAge;
    @BindView(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @BindView(R.id.tv_boolean_self_patient)
    TextView tvBooleanSelfPatient;
    @BindView(R.id.tv_service_address)
    TextView tvServiceAddress;
    @BindView(R.id.tv_service_address_right)
    TextView tvServiceAddressRight;
    @BindView(R.id.tv_service_time)
    TextView tvServiceTime;
    @BindView(R.id.tv_service_time_right)
    TextView tvServiceTimeRight;
    @BindView(R.id.tv_desc_label)
    TextView tvDescLabel;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.tv_bottom_btn)
    TextView tvBottomBtn;
    @BindView(R.id.imv_desc_pic)
    ImageView imv_desc_pic;
    private INurse model;
    String orderId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_order_details;
    }

    @Override
    public void setupView() {
        model = new NurseModel();
        ButterKnife.bind(this);
        tvTitle.setText("订单详情");
        orderId = getIntent().getStringExtra("key_id");
        refreshOrder();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        NurseHomeApp.getInstance().finishActivity(this);
    }

    @Override
    public void setupData() {

    }

    public void updateOrderStatus(String orderStatus) {
        model.UpdateOrderStatus(orderId, orderStatus).subscribe(new CommonSubscriber<BaseResultBean>() {
            @Override
            public void onSuccess(BaseResultBean response) {
                refreshOrder();
            }

            @Override
            public void onFailure(BaseResultBean response) {
                Toasty.error(OrderDetailActivity.this, response.getMsg(), Toast.LENGTH_SHORT, true).show();
            }
        });
    }

    private void refreshOrder(){
        model.GetOrderDetail(orderId).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                showDialog();
            }
        }).subscribe(new CommonSubscriber<OrderDetailBean>() {
            @Override
            public void onSuccess(OrderDetailBean response) {
                hideDialog();
                Glide.with(OrderDetailActivity.this).load(response.getServiceOrder().getOrderPic()).into(imvHeader);
                tvOrderNum.setText("订单编号: " + response.getServiceOrder().getOrderNo());
                tvServiceName.setText("服务项目: " + response.getServiceOrder().getOrderName());
                String serviceStatus = response.getServiceOrder().getServiceStatus();
                if ("all".equals(serviceStatus)) {
                    serviceStatus = "";//全部
                } else if ("waitConfirm".equals(serviceStatus)) {
                    serviceStatus = "待接单";//待接单
                    tvBottomBtn.setText("确认接单");
                    tvBottomBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            updateOrderStatus("waitStartOff");
                        }
                    });
                } else if ("waitStartOff".equals(serviceStatus)) {
                    tvBottomBtn.setText("确定出发");
                    serviceStatus = "待出诊";//待出诊
                    tvBottomBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            updateOrderStatus("startOff");
                        }
                    });
                } else if ("startOff".equals(serviceStatus)) {
                    serviceStatus = "已出诊";//已出诊
                    tvBottomBtn.setText("确定到达");
                    tvBottomBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            updateOrderStatus("arrived");
                        }
                    });
                } else if ("arrived".equals(serviceStatus)) {
                    serviceStatus = "已到达";//已到达
                    tvBottomBtn.setText("开始服务");
                    tvBottomBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            updateOrderStatus("startService");
                        }
                    });
                } else if ("startService".equals(serviceStatus)) {
                    serviceStatus = "服务中";//已到达
                    tvBottomBtn.setText("结束服务");
                    tvBottomBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            updateOrderStatus("serviceFinish");
                        }
                    });
                } else if ("serviceFinish".equals(serviceStatus)) {
                    serviceStatus = "已结束";//已结束
                    tvBottomBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            OrderDetailActivity.this.finish();
                        }
                    });
                    tvBottomBtn.setVisibility(View.INVISIBLE);
                }
                else{
                    OrderDetailActivity.this.finish();
                }
                tvOrderStatus.setText("订单状态:"+serviceStatus);
                tvPatientName.setText(response.getServiceOrder().getPatientName());
                tvPatientAge.setText("患者年龄: " + response.getServiceOrder().getPatientAge() + "岁");
                if (TextUtils.isEmpty(response.getServiceOrder().getPatientMedicalId())) {
                    tvBooleanSelfPatient.setText("不是本院患者");
                } else {
                    tvBooleanSelfPatient.setText("是本院患者");
                }
                tvServiceAddressRight.setText(response.getServiceOrder().getServiceAddress());
                tvServiceTimeRight.setText(response.getServiceOrder().getServiceDate());
                tvDesc.setText(response.getServiceOrder().getDescription());
                Glide.with(OrderDetailActivity.this).load(response.getServiceOrder().getDescriptionPic()).into(imv_desc_pic);
                imv_desc_pic.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent=new Intent(OrderDetailActivity.this,PicViewActivity.class);
                        ArrayList<String> imgUrls=new ArrayList<>();
                        imgUrls.add(response.getServiceOrder().getDescriptionPic());
                        intent.putStringArrayListExtra("key_img_urls",imgUrls);
                        startActivity(intent);
                    }
                });
            }

            @Override
            public void onFailure(BaseResultBean response) {
                hideDialog();
                Toasty.error(OrderDetailActivity.this, response.getMsg(), Toast.LENGTH_SHORT, true).show();
            }
        });
    }

}
